package org.amphiprion.descentv2assistant.ui.views.controler;

import org.amphiprion.descentv2assistant.R;

import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.AnimatorListenerAdapter;
import com.nineoldandroids.view.ViewHelper;
import com.nineoldandroids.view.ViewPropertyAnimator;

public class UndoBarController {
	private ViewPropertyAnimator mBarAnimator;
	private View mBarView;
	private Handler mHideHandler = new Handler();
	private Runnable mHideRunnable = new Runnable() {
		@Override
		public void run() {
			UndoBarController.this.hideUndoBar(false);
		}
	};
	private TextView mMessageView;
	protected int mPosition;
	private UndoListener mUndoListener;
	private CharSequence mUndoMessage;
	private Object mUndoToken;

	public UndoBarController(View paramView, UndoListener paramUndoListener) {
		this.mBarView = paramView;
		this.mBarAnimator = ViewPropertyAnimator.animate(this.mBarView);
		this.mUndoListener = paramUndoListener;
		this.mMessageView = ((TextView) this.mBarView
				.findViewById(R.id.undobar_message));
		this.mBarView.findViewById(R.id.undobar_button).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View paramAnonymousView) {
						UndoBarController.this.hideUndoBar(false);
						UndoBarController.this.mUndoListener.onUndo(
								UndoBarController.this.mUndoToken,
								UndoBarController.this.mPosition);
					}
				});
		hideUndoBar(true);
	}

	public void hideUndoBar(boolean quick) {
		this.mHideHandler.removeCallbacks(this.mHideRunnable);
		if (quick) {
			this.mBarView.setVisibility(View.GONE);
			ViewHelper.setAlpha(this.mBarView, 0.0F);
			this.mUndoMessage = null;
			this.mUndoToken = null;
		} else {
			this.mBarAnimator.cancel();
			this.mBarAnimator.alpha(0.0F).setDuration(300)
					.setListener(new AnimatorListenerAdapter() {
						@Override
						public void onAnimationEnd(
								Animator paramAnonymousAnimator) {
							UndoBarController.this.mBarView
									.setVisibility(View.GONE);
							UndoBarController.this.mUndoMessage = null;
							UndoBarController.this.mUndoToken = null;
						}
					});
		}

	}

	public void onRestoreInstanceState(Bundle paramBundle) {
		if (paramBundle != null) {
			this.mUndoMessage = paramBundle.getCharSequence("undo_message");
			this.mUndoToken = paramBundle.getParcelable("undo_token");
			if ((this.mUndoToken != null)
					|| (!TextUtils.isEmpty(this.mUndoMessage))) {
				showUndoBar(true, this.mUndoMessage, this.mUndoToken,
						this.mPosition);
			}
		}
	}

	public void showUndoBar(boolean quick, CharSequence paramCharSequence,
			Object paramObject, int paramInt) {
		this.mUndoToken = paramObject;
		this.mUndoMessage = paramCharSequence;
		this.mMessageView.setText(this.mUndoMessage);
		this.mHideHandler.removeCallbacks(this.mHideRunnable);
		this.mHideHandler.postDelayed(this.mHideRunnable, 5000L);
		this.mBarView.setVisibility(0);
		if (quick) {
			ViewHelper.setAlpha(this.mBarView, 1.0F);
		} else {
			this.mBarAnimator.cancel();
			this.mBarAnimator.alpha(1.0F).setDuration(300).setListener(null);
		}
	}

	public static abstract interface UndoListener {
		public abstract void onUndo(Object paramObject, int paramInt);
	}
}